<template>
	<view class="container">
    <view class="login" @tap="onScanCode">
      <view class="title">
        <view class="w100">Hello,</view>
        <view class="w100 welcome">欢迎登录元集校服订购商城</view>
      </view>
      <view class="agreement">
        <checkbox
          :checked="checked"
          color="#105c3e"
          @tap="onSelect"
        />
        同意已阅读<text>《用户协议》</text>和<text>《个人信息保护政策》</text>
      </view>
      <button class="login-btn" open-type="getUserInfo" @getuserinfo="getUserInfo">手机号快捷登录</button>
    </view>
	</view>
</template>

<script setup>
const checked = ref(false)
const onSelect = () => {
  checked.value = !checked.value
}

const getUserInfo = (e) => {
  var p = getSetting()
  p.then(function(isAuth) {
  	console.log('是否已经授权', isAuth);
  	if (isAuth) {
      uni.login({
        provider: 'weixin',
        success: (res) => {
          console.log('用户信息，加密数据', e);
          const { encryptedData, iv, rawData, signature } = e.detail
          //eData  包括//微信头像//微信名称 还有加密的数据.
          let eData = JSON.parse(rawData);
          //接下来就是访问接口.
          http.request({
          	header: { 'content-type': 'application/x-www-form-urlencoded' },
          	url: '/base/user/login', // 你的接口地址
          	method: 'POST',// 接口类型
          	data: {
              code: res.code,
              user_info: e.detail.rawData,
              encrypted_data: encryptedData,
              iv: iv,
              signature: e.detail.signature
            }, // 接口需要的数据
          	success: function(result) {
          		console.log(result);
          		if (result.data.Success) {
          			// that.login(res.data); //将接口返回的数据保存在全局变量中.
          			//登录成功跳转首页或者你想跳转的地方...
          			//注意：如果时导航页 请用uni.switchTab
          			// 		其他页面建议使用uni.reLaunch
          		} else {
          			uni.showToast({
          				title: '授权登录失败！',
          				mask: true,
          				icon: 'none'
          			})
          		}
          	}
          })
        },
        fail: (error) => {
          console.error('登录失败：', error);
        }
      })
  	} else {
  		uni.showToast({
  			title: '授权失败，请确认授权已开启',
  			mask: true,
  			icon: 'none'
  		})
  	}
  })
}
//获取用户的当前设置
const getSetting = () => {
  return new Promise(function(resolve, reject) {
  	uni.getSetting({
  		success: function(res) {
  			if (res.authSetting['scope.userInfo']) {
  				console.log('存在');
  				resolve(true);
  			} else {
  				console.log('不存在');
  				resolve(false);
  			}
  		}
  	})
  }).catch((e) => {
  	console.log(e)
  })
}
</script>

<style lang="scss" scoped>
.container{
  padding: 30rpx;
  width: 100%;
  height: 100vh;
  box-sizing: border-box;
  .w100{
    width:100%;
  }
  .title{
    width: 100%;
    color: #333;
    font-size: 40rpx;
    font-weight: bold;
    text-align: center;
    .welcome{
      margin-top: 10rpx;
    }
  }
  .agreement{
    margin-top: 200rpx;
    color: #333;
    font-size: 28rpx;
    text{
      color: #fc0130;
    }
  }
  .login-btn{
    margin-top: 24rpx;
    padding: 0 30rpx;
    height: 84rpx;
    line-height: 84rpx;
    color: #fff;
    font-size: 32rpx;
    text-align: center;
    border-radius: 84rpx;
    background: #07c160;
  }
}
</style>
